package gqqnbig;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.concurrent.Callable;

public class ErrorStreamReaderThread implements Runnable
{

	private InputStream ins;
	private PrintStream outStream;
	private Callable<PrintStream> getPrintStream;

	public ErrorStreamReaderThread(InputStream ins, Callable<PrintStream> getPrintStream)
	{
		this.ins = ins;
		this.getPrintStream=getPrintStream;
	}

	@Override
	public void run()
	{
		BufferedReader reader1 = new BufferedReader(new InputStreamReader(ins));
		try
		{
			String line = reader1.readLine();
			while (line != null)
			{
				if(outStream==null)
					outStream=getPrintStream.call();
				outStream.println(line);
				line = reader1.readLine();
			}
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	}

}